package com.serkol.abmanager.controller.api;

import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.bean.Result;
import com.serkol.abmanager.config.ConstantHeader;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.*;
import com.serkol.abmanager.service.AutomobilesLogService;
import com.serkol.abmanager.service.AutomobilesService;
import com.serkol.abmanager.service.GoodsService;
import com.serkol.abmanager.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/autobobiles")
@ResponseBody
public class AutoMobilesApiController {

    @Autowired
    private AutomobilesService service;

    @Autowired
    private AutomobilesLogService logService;

    @Autowired
    private UsersService usersService;

    @PostMapping("/findByList")
    public GridResult findByList(@RequestParam(name = "brand", defaultValue = "") String brand,@RequestParam(name = "carNum", defaultValue = "") String carNum,@RequestParam(name = "model", defaultValue = "") String model,@RequestParam(name = "groupId", defaultValue = "0") long groupId,@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "pagesize", defaultValue = "10") int pagesize) {

        GridResult page1 = service.findByList(page, pagesize,brand,carNum,model,groupId);

        return page1;
    }

    @PostMapping("/findById")
    public Result findById(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            Automobiles byId = service.getById(id);
            if (byId != null) {
                return Result.ok(byId);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @PostMapping("/findByDelete")
    public Result findByDelete(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            boolean b = service.removeById(id);
            if (b) {
                return Result.ok();
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @PostMapping("/edit")
    public Result edit(@RequestParam(name = "id", defaultValue = "0") long id,
                       @RequestParam(name = "brand", defaultValue = "") String brand,
                       @RequestParam(name = "model", defaultValue = "") String model,
                       @RequestParam(name = "carNum", defaultValue = "") String carNum,
                       @RequestParam(name = "onLoad", defaultValue = "") String onLoad,
                       @RequestParam(name = "groupId", defaultValue = "") long groupId,
                       @RequestParam(name = "groupType", defaultValue = "") int groupType,
                       @RequestParam(name = "licensePlateNum", defaultValue = "") String licensePlateNum
    ) {
        Result result = new Result();
        if (!brand.isEmpty() && !model.isEmpty() && !onLoad.isEmpty() && !carNum.isEmpty()) {
            Automobiles goods = new Automobiles();
            goods.setBrand(brand);
            goods.setModel(model);
            goods.setCarNum(carNum);
            goods.setOnLoad(onLoad);
            goods.setGroupId(groupId);
            goods.setGroupType(groupType);
            goods.setLicensePlateNum(licensePlateNum);
            boolean flag = false;
            if (id > 0) {
                goods.setId(id);
                flag = service.updateById(goods);
            } else {
                flag = service.save(goods);
            }
            if (flag) {
                result.setStatus(ResultConstant.SUCSESS);
            } else {
                result.setStatus(ResultConstant.FAIL);
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }

    @PostMapping("/carChange")
    public Result carChange(@RequestParam(name = "id", defaultValue = "0") long id,
                            @RequestParam(name = "newCarId", defaultValue = "0") long newCarId,
//                            @RequestParam(name = "oldCarId", defaultValue = "0") long oldCarId,
                            @RequestParam(name = "remark", defaultValue = "") String remark,
                            @RequestParam(name = "userId", defaultValue = "") long userId
    ) {
        Result result = new Result();
        if (userId > 0 && newCarId > 0) {
            Users byId = usersService.getById(userId);
            if (byId != null) {
                Automobiles byId1 = service.getById(newCarId);
                if (byId1 != null) {
                    Users goods = new Users();
                    goods.setCarId(newCarId);
                    boolean flag = false;
                    goods.setId(userId);
                    flag = usersService.updateById(goods);

                    if (flag) {
                        AutomobilesLog tracksLog = new AutomobilesLog();
                        tracksLog.setUserId(userId);
                        tracksLog.setNewCarId(newCarId);
                        tracksLog.setOldCarId(byId.  getCarId());
                        tracksLog.setRemark(remark);
                        tracksLog.setOpratorId(ConstantHeader.getAdminToken().getUserId());
                        boolean save = logService.save(tracksLog);
                        if (save) {

                            result.setStatus(ResultConstant.SUCSESS);
                        } else {
                            result.setStatus(ResultConstant.FAIL);
                        }
                    } else {
                        result.setStatus(ResultConstant.FAIL);
                    }
                } else {
                    result.setStatus(ResultConstant.EMPTIY);
                    result.setMsg("新汽车不存在");
                }
            } else {
                result.setStatus(ResultConstant.EMPTIY);
                result.setMsg("人员不存在");
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }


}
